﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.WinService;
using DevExpress.XtraRichEdit.API.Native;

namespace ECRMS.WinForm.Modules.BaseRibbonControl
{
    public partial class ReportOutModule : DevExpress.XtraEditors.XtraForm
    {
        private ReportDesignModule reportDesignModule;
        private RibbonModule ContentModule;

        public ReportOutModule(ReportDesignModule reportDesignModule)
        {
            this.reportDesignModule = reportDesignModule;
            InitializeComponent();
            InitView();
            InitData();
        }

        private void InitData()
        {
            //StringBuilder builder = new StringBuilder();
            //foreach (CtReport item in this.reportDesignModule.core.CoreReportOfDesign.Reports)
            //{
            //    builder.Append(GetRealContent(System.Text.Encoding.UTF8.GetString(item.Content.ToArray())));
            //}
            //string rtfStartFormat = this.ContentModule.richEditControl.RtfText.Remove(this.ContentModule.richEditControl.RtfText.Length - 9, 9);
            //string rtfEndFormat = this.ContentModule.richEditControl.RtfText.Remove(0,267);

            //this.ContentModule.richEditControl.BeginUpdate();
            //this.ContentModule.richEditControl.RtfText = rtfStartFormat + builder.ToString() + rtfEndFormat;
            //this.ContentModule.richEditControl.EndUpdate();

            foreach (CtReport item in this.reportDesignModule.core.CoreReportOfDesign.Reports)
            {
                RibbonModule control = this.reportDesignModule.Controls.Find("rec" + item.ReportPartRefId.ToString(), true)[0] as RibbonModule;
                DocumentPosition position =this.ContentModule.richEditControl.Document.CreatePosition(this.ContentModule.richEditControl.RtfText.Length);
                this.ContentModule.richEditControl.Document.InsertRtfText(position, control.richEditControl.RtfText);
            }
        }

        //去掉外面的封装
        private string GetRealContent(string str)
        {
            str = str.Remove(0, 267);
            str= str.Remove(str.Length -9, 9);
            str = str + "\\cf0\\par\\pard\\plain\\ql";
            return str;
        }

        private void InitView()
        {
            this.Width = 900;
            this.Height = 700;
            ContentModule = new RibbonModule()
            {
                Dock = DockStyle.Fill
            };
            this.Controls.Add(ContentModule);
        }
    }
}